#!/usr/bin/env python
# @desc : ES元数据信息对象
"""
@dataclass 标识class时，代表该class是一个数据对象，可以直接有属性，不需要单独设置 @property @setter
"""


from dataclasses import dataclass


@dataclass
class ESMeta:
    inType: str
    esNodes: str
    esIndex: str
    esType: str
    selectFields: str

    def __init__(self, inType, esNodes, esIndex, esType, selectFields):
        self.inType = inType
        self.esNodes = esNodes
        self.esIndex = esIndex
        self.esType = esType
        self.selectFields = selectFields

    def fromDictToEsMeta(ruleDict: dict):
        return ESMeta(ruleDict.get("inType", ""),
                      ruleDict.get("esNodes", ""),
                      ruleDict.get("esIndex", ""),
                      ruleDict.get("esType", ""),
                      ruleDict.get("selectFields", ""))

def ruleToESMeta(rule):
    """
    标签规则数据转换为ESMeta对象的方法
    :param rule: 标签规则数据，这是一个字符串
    :return: ESMeta对象
    """
    #列表中一共有5个值，格式是：key=value，比如inType=Elasticsearch
    lists = rule.split("##")
    ruleDict = {}
    for ruleKV in lists:
        #kvStr[0] = key
        #kvStr[1] = value
        kvStr = ruleKV.split("=")
        ruleDict[kvStr[0]] = kvStr[1]
    # **：专门解析转换dict类型的数据
    return ESMeta(**ruleDict)
